import Notify from '@vant/weapp/notify/notify';
const app = getApp();
Page({

  data: {
    name: '',
    phone: '',
    emial: '',
    number_id: '',
    front_pics: [],
    back_pics: [],
    click_button: 0,
    ocrInfoFront: {
      name: "",
      number_id: "",
      address: "",
      nation: "", //民族
      sex: "",
      birthday: ""
    },
    fileToken: '',
    role_id: 3,
    idcardFrontImage: "",
    idCards: []
  },

  onLoad: function (options) {
    this.getFileToken()
  },

  getFileToken() {
    let that = this
    wx.request({
      url: app.globalData.domain + 'uploads/upload_key/',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        that.setData({
          fileToken: res.data.data.key
        })
      }
    })
  },
  idCardDelete(event) {
    this.setData({
      idcards: []
    })
  },
  afterRead(event) {
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    var that = this;
    wx.uploadFile({
      url: app.globalData.domain + 'uploads/?upcheck=' + that.data.fileToken + '&up_type=number_id_img', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      // formData: { user: 'test' },
      success(res) {
        var data = JSON.parse(res.data)
        // 上传完成需要更新 fileList
        let imageInfo = {
          url: app.globalData.domain + "file/" + data.data[0].file_url,
          name: '身份证正面',
          isImage: true,
          deletable: true,
        }
        that.setData({
          idcardFrontImage: imageInfo.url
        })
        let fileList = []
        fileList.push(imageInfo);
        that.setData({
          idcards: fileList
        });
        wx.request({
          url: app.globalData.domain + 'interviewer/ocr_sfz/',
          method: "POST",
          data: {
            idCardSide: 'front',
            url: imageInfo.url
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            if (res.data.response === 'ok') {
              Notify({
                type: 'primary',
                message: '身份证上传成功'
              });
              let ocrInfo = res.data.data;
              that.setData({
                ocrInfoFront: ocrInfo
              })
              that.setData({
                ocr_front: ocrInfo
              })
            } else {
              Notify({
                type: 'danger',
                message: '身份证识别失败:' + res.data.message
              });
            }
          }
        })
      }
    })
  },
  saveStep1(is_stop) {
    if (is_stop != 1) {
      is_stop = 0
      console.log("不是暂存")
    } else {
      console.log("暂存")
    }
    if (!this.data.idcardFrontImage) {
      Notify({
        type: 'danger',
        message: '请上传身份证正面'
      });
      return false
    }
    let idCardInfo = {
      number_id: this.data.ocrInfoFront.number_id,
      name: this.data.ocrInfoFront.name,
      address: this.data.ocrInfoFront.address,
      card: this.data.idcardFrontImage,
    }
    let post_data = {
      "role_id": this.data.role_id,
      "form": JSON.stringify(idCardInfo)
    }
    var that = this;
    wx.request({
      url: app.globalData.domain + 'interviewer/new_save/?role',
      method: "POST",
      data: post_data,
      header: {
        "Token": app.getToken(),
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      success: function (res) {
        if (res.data.response == "ok") {
          app.setToken(res.data.data.token)
          app.setUserId(res.data.data.user_id)
          app.setRole("用户")
          app.setAdminName(that.data.phone)
          app.setSessionTable("yonghu")
        } else {
          Notify({
            type: 'danger',
            message: res.data.message
          });
        }
      }
    })
  },

})